package com.foreveross.bsl.mam.app.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.foreveross.bsl.common.utils.web.model.PageMustache;
import com.foreveross.bsl.mam.app.application.AppMainApplication;
import com.foreveross.bsl.mam.app.application.SettingCategoryApplication;
import com.foreveross.bsl.mam.app.application.vo.AppMainVo;
import com.foreveross.bsl.mam.app.application.vo.SettingCategoryVo;
import com.foreveross.bsl.mam.model.application.WidgetApplication;
import com.foreveross.bsl.mam.model.application.vo.WidgetVO;
import com.foreveross.bsl.web.BaseController;

/**
 * 系统设置操作类
 * 
 * @author tanzhixiong
 * @2013-6-22 @下午3:01:42
 */
@Controller
@RequestMapping("/mam/setting")
public class SettingController extends BaseController {

	@Inject
	private SettingCategoryApplication settingCategoryApplication;
	
	@Inject
	private AppMainApplication appMainApplication;
	
	@Inject
	private WidgetApplication widgetApplication;

	/**
	 * 此方法描述的是：检查名称是否存在
	 * 
	 * @author: panhm@foreveross.com
	 * @version: 2013-6-22 下午4:55:28
	 */
	@ResponseBody
	@RequestMapping("/check/{type}")
	public Map<String, String> checkName(@RequestParam("name") String name,
			@PathVariable("type") String type) {

		Map<String, String> result = new HashMap<String, String>();
		SettingCategoryVo res = settingCategoryApplication
				.checkName(name, type);
		if (res != null) {
			result.put("result", "true");
		} else {
			result.put("result", "false");
		}

		return result;
	}

	@RequestMapping("/index/{type}")
	public String index(@PathVariable("type") String type, Model model,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "page.size", defaultValue = "10") int pageSize) {

		Pageable pageable = new PageRequest(pageNumber - 1, pageSize, new Sort(
				Direction.DESC, "modified_time"));
		Page<SettingCategoryVo> list = settingCategoryApplication
				.findByPageable(type, pageable);
		// List<SettingCategoryVo> list = settingCategoryApplication.findByType(
		// type, "1");
		model.addAttribute("list", list);
		model.addAttribute("type", type);
		model.addAttribute("page", new PageMustache(list, 10));
		return "setting/index";
	}

	@RequestMapping("/enterAdd/{type}")
	public String enterAdd(@PathVariable("type") String type, Model model) {
		// List<SettingCategoryVo> list = settingCategoryApplication.findByType(
		// type, "1");
		// model.addAttribute("list", list);
		model.addAttribute("type", type);
		return "setting/add";
	}

	@ResponseBody
	@RequestMapping("/add")
	public Map<String, String> add(SettingCategoryVo settingVo) {
		Map<String, String> result = new HashMap<String, String>();
		settingVo.setAvailable("1");
		Date date = new Date();
		settingVo.setCreate_time(date);
		settingVo.setModified_time(date);
		SettingCategoryVo vo = settingCategoryApplication.saveEntity(settingVo);
		result.put("result", "success");
		result.put("type", vo.getType());
		return result;
	}

	@RequestMapping("/enterUpdate/{id}")
	public String enterUpdate(@PathVariable("id") String id, Model model) {
		SettingCategoryVo vo = settingCategoryApplication.getEntity(id);
		model.addAttribute("settingVo", vo);
		return "setting/edit";
	}

	@ResponseBody
	@RequestMapping("/update")
	public Map<String, String> update(SettingCategoryVo settingVo) {
		Map<String, String> result = new HashMap<String, String>();
		SettingCategoryVo vo = settingCategoryApplication.getEntity(settingVo
				.getId());
		vo.setName(settingVo.getName());
		vo.setModified_time(new Date());
		SettingCategoryVo resultVo = settingCategoryApplication
				.updateEntity(vo);
		result.put("result", "success");
		result.put("type", resultVo.getType());
		return result;
	}

	@ResponseBody
	@RequestMapping("/delete")
	public Map<String, Object> delete(String[] ids, String type) {
		Map<String, Object> result = new HashMap<String, Object>();
		boolean checkOK = true;
		if (ids != null) {
			if ("1".equals(type)) {
				checkOK = checkApp(ids);
			}else if("2".equals(type)){
				checkOK = checkModel(ids);
			}
			
			if (checkOK) {
				for (int i = 0; i < ids.length; i++) {
					settingCategoryApplication.deleteEntity(ids[i]);
				}
			}else{
				result.put("result", "false");
				result.put("type", type);
				return result;
			}
			
		}
		result.put("result", "success");
		result.put("type", type);
		return result;
	}

	@Override
	protected String getModuleMenuName() {
		// TODO Auto-generated method stub
		return "系统设置";
	}

	private boolean checkApp(String[] ids){
		List<AppMainVo> appList = null;
		for (String id : ids) {
			appList = appMainApplication.getEntitybyCategoryId(id);
			if (appList != null && !appList.isEmpty()) {
				return false;
			}
		}
		return true;
	}
	
	private boolean checkModel(String[] ids){
		List<WidgetVO>  widgetList = null;
		for (String id : ids) {
			widgetList = widgetApplication.findByCategoryId(id);
			if (widgetList != null && !widgetList.isEmpty()) {
				return false;
			}
		}
		return true;
	}
}
